//
//  HFWelcomController.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HFWelcomController: UIViewController {
    
    override func loadView() {
        //
        view = backImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()

        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        startAnimation()
    }
    
    //添加动画效果
    private func startAnimation() {
        iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
                })
        }
    }
    
    //添加并布局控件们
    private func setUpUI() {
        //添加控件
        view.addSubview(iconImage)
        view.addSubview(welcomLabel)
        
        //设置约束
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //加载头像图片
        iconImage.sd_setImageWithURL(HFUserAccountViewModel().headImageUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        //处理圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }
    
    //懒加载控件们
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    private lazy var iconImage: UIImageView = UIImageView()
    private lazy var welcomLabel: UILabel = {
        let label = UILabel()
        
        label.text = (HFUserAccountViewModel().userName ?? "") + "欢迎回来"
        label.textColor = UIColor.lightGrayColor()
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(19)
        label.alpha = 0
        label.sizeToFit()
        
        return label
    }()

}
